package com.shunhao.utils

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.os.Handler
import android.view.KeyEvent

/**
 * <pre>
 * 文件名：	AlertDialogUtil
 * 描　述：	AlertDialog工具类
 * @author 阿贝斯强哥
</pre> *
 */
object AlertDialogUtil {

    /**
     * showAlertDialog
     * @param context    上下文对象
     * @param msg        弹框信息
     * @param msgHandler 处理handler
     */
    fun showAlertDialog(context: Context?, msg: String?, msgHandler: Handler?) {
        try {
            val dialog = AlertDialog.Builder(context).create()
            dialog.setCanceledOnTouchOutside(false)
            dialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
                if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH
                    || keyCode == KeyEvent.KEYCODE_MENU
                ) {
                    return@setOnKeyListener true
                }
                false
            }
            dialog.setTitle("温馨提示")
            dialog.setMessage(msg)
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消") { dialog12: DialogInterface, _: Int ->
                dialog12.dismiss()
                msgHandler?.sendEmptyMessage(0)
            }
            dialog.setButton2("确定") { dialog1: DialogInterface, _: Int ->
                dialog1.dismiss()
                msgHandler?.sendEmptyMessage(1)
            }
            dialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * @param context    上下文对象
     * @param msg        提示信息
     * @param title      标题信息
     * @param sure       确认按钮文字
     * @param cancel     取消按钮文字
     * @param msgHandler 处理handler
     */
    fun showAlertDialog(
        context: Context?,
        msg: String?,
        title: String?,
        sure: String?,
        cancel: String?,
        msgHandler: Handler?
    ) {
        try {
            val dialog = AlertDialog.Builder(context).create()
            dialog.setCanceledOnTouchOutside(false)
            dialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
                if (keyCode == KeyEvent.KEYCODE_BACK
                    || keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_MENU
                ) {
                    return@setOnKeyListener true
                }
                false
            }
            dialog.setTitle(title)
            dialog.setMessage(msg)
            dialog.setButton(cancel) { dialog12: DialogInterface, _: Int ->
                dialog12.dismiss()
                msgHandler?.sendEmptyMessage(0)
            }
            dialog.setButton2(sure) { dialog1: DialogInterface, _: Int ->
                dialog1.dismiss()
                msgHandler?.sendEmptyMessage(1)
            }
            dialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * @param context    上下文对象
     * @param msg        提示信息
     * @param title      标题
     * @param sure       确认按钮
     * @param cancel     取消按钮文字
     * @param cancelAble 是否可以取消
     * @param msgHandler 处理handler
     */
    fun showAlertDialog(
        context: Context?,
        msg: String?,
        title: String?,
        sure: String?,
        cancel: String?,
        cancelAble: Boolean,
        msgHandler: Handler?
    ) {
        try {
            val dialog = AlertDialog.Builder(context).create()
            dialog.setCanceledOnTouchOutside(false)
            if (!cancelAble) {
                dialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
                    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH
                        || keyCode == KeyEvent.KEYCODE_MENU
                    ) {
                        return@setOnKeyListener true
                    }
                    false
                }
            }
            dialog.setTitle(title)
            dialog.setMessage(msg)
            dialog.setButton(cancel) { dialog12: DialogInterface, _: Int ->
                dialog12.dismiss()
                msgHandler?.sendEmptyMessage(0)
            }
            dialog.setButton2(sure) { dialog1: DialogInterface, _: Int ->
                dialog1.dismiss()
                msgHandler?.sendEmptyMessage(1)
            }
            dialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 只有一个按钮的dialog
     *
     * @param context    上下文对象
     * @param msg        内部消息
     * @param msgHandler 处理handler
     */
    fun showSingleAlertDialog(
        context: Context?, msg: String?, msgHandler: Handler?
    ) {
        try {
            val dialog = AlertDialog.Builder(context).create()
            dialog.setCanceledOnTouchOutside(false)
            dialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
                if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH
                    || keyCode == KeyEvent.KEYCODE_MENU
                ) {
                    return@setOnKeyListener true
                }
                false
            }
            dialog.setTitle("温馨提示")
            dialog.setMessage(msg)
            dialog.setButton("确定") { dialog12: DialogInterface, _: Int ->
                if (msgHandler != null) {
                    dialog12.dismiss()
                    msgHandler.sendEmptyMessage(1)
                }
            }
            dialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 只有一个按钮的dialog
     *
     * @param context    上下文对象
     * @param msg        提示信息
     * @param title      标题内容
     * @param sure       确认按钮
     * @param msgHandler 处理handler
     */
    fun showSingleAlertDialog(
        context: Context?,
        msg: String?,
        title: String?,
        sure: String?,
        msgHandler: Handler?
    ) {
        try {
            val dialog = AlertDialog.Builder(context).create()
            dialog.setCanceledOnTouchOutside(false)
            dialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
                if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH
                    || keyCode == KeyEvent.KEYCODE_MENU
                ) {
                    return@setOnKeyListener true
                }
                false
            }
            dialog.setTitle(title)
            dialog.setMessage(msg)
            dialog.setButton(sure) { dialog1: DialogInterface, _: Int ->
                if (msgHandler != null) {
                    dialog1.dismiss()
                    msgHandler.sendEmptyMessage(1)
                }
            }
            dialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 只有一个按钮的dialog
     *
     * @param context    上下文对象
     * @param msg        提示信息
     * @param title      标题内容
     * @param sure       确认按钮文字
     * @param msgHandler 处理handler
     */
    fun showSingleAlertDialog(
        context: Context?,
        msg: String?,
        title: String?,
        sure: String?,
        cancelAble: Boolean,
        msgHandler: Handler?
    ) {
        try {
            val dialog = AlertDialog.Builder(context).create()
            dialog.setCanceledOnTouchOutside(false)
            if (!cancelAble) {
                dialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
                    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH
                        || keyCode == KeyEvent.KEYCODE_MENU) {
                        return@setOnKeyListener true
                    }
                    false
                }
            }
            if (title != null) {
                dialog.setTitle(title)
            }
            dialog.setMessage(msg)
            dialog.setButton(sure) { _: DialogInterface?, _: Int ->
                msgHandler?.sendEmptyMessage(1)
            }
            dialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}